<?php
require_once 'buildCurl.php';
require_once 'conn.php';
$res = json_decode(file_get_contents('php://input'), true);

$session = $res['session'];

$class_id = $res['class_id'];
$day = $res['day'];
$school = $res['school'] + 1;
$week = $res['week'];

if (!empty($res['enforce'])) $enforce = $res['enforce'];
else $enforce = 0;


$sql = "SELECT * FROM `kongke` WHERE `school` = " . $school . " AND `class` = " . $class_id . " AND `day` = " . $day . " AND `week` = " . $week;
$tis = $pdo->query($sql);
$rows = $tis->fetchAll();
$rowCount = count($rows);
if ($rowCount > 0) {
    $uptime =  $rows[0]['uptime'];
    $content =  $rows[0]['content'];
    if ($enforce == 0 && time() - $uptime < 3600 * 14) {
        $tis = null;
        $pdo = null;
        exit($content);
    }
}


$pagenum = 1;
$arr = [['主楼', '阶教', '操场', '大数据', '矿山压力', '力学楼', '煤矿基地', '实1', '实2', '实3', '实习工厂', '图书馆', '行知楼'], ['2号教学楼', '办公楼', '操场', '机房', '教学楼', '阶梯']];

$teaNum = $arr[$res['school']][$res['class_id']];

$url = "http://211.82.48.36/student/teachingResources/freeClassroomQuery/search";
$post = "weeks=" . $week . "&jslxdm=&codeCampusListNumber=00" . $school . "&teaNum=" . $teaNum . "&wSection=" . $day . "%2F1&pageNum=" . $pagenum . "&pageSize=40";

$myCurl = new MyPostCurl($url, $session, $post, 0);
$result = $myCurl->getResult();
$httpCode = $myCurl->getHttpCode();


if ($httpCode != 200) {
    $arr = array('err' => 1);
    exit(json_encode($arr));
}

$class_all = [];

for ($course = 1; $course <= 5; $course++) {

    $class_all[$course - 1] = get_class_arr($week, $school, $teaNum, $day, 2 * $course - 1, $pagenum, $session);
}

function get_class_arr($week, $school, $teaNum, $day, $course, $pagenum, $session)
{
    $big_string = "";
    $post = "weeks=" . $week . "&jslxdm=&codeCampusListNumber=00" . $school . "&teaNum=" . rawurlencode($teaNum) . "&wSection=" . $day . "%2F" . $course . "," . $day . "%2F" . ($course + 1) . "&pageNum=" . $pagenum . "&pageSize=100";
    $url = "http://211.82.48.36/student/teachingResources/freeClassroomQuery/search";

    $myCurl = new MyPostCurl($url, $session, $post, 0);
    $result = $myCurl->getResult();

    $content = json_decode($result, true);
    $total = $content[0]['pageContext']['totalCount'];
    $list0 = $content[0]['records'];
    if (is_array($list0)) {
        foreach ($list0 as $value0) {
            $classroomName = $value0['classroomName'];
            $big_string =  $big_string . "," . $classroomName;
        }
    }

    $endpage = ceil($total / 100);




    for ($i = 2; $i <= $endpage; $i++) {
        $post = "weeks=" . $week . "&jslxdm=&codeCampusListNumber=00" . $school . "&teaNum=" . rawurlencode($teaNum) . "&wSection=" . $day . "%2F" . $course . "," . $day . "%2F" . ($course + 1) . "&pageNum=" . $i . "&pageSize=100";
        $url = "http://211.82.48.36/student/teachingResources/freeClassroomQuery/search";

        $myCurl = new MyPostCurl($url, $session, $post, 0);
        $result = $myCurl->getResult();

        $content = json_decode($result, true);
        $list0 = $content[0]['records'];
        foreach ($list0 as $value0) {
            $classroomName = $value0['classroomName'];
            $big_string = $big_string . "," . $classroomName;
        }
    }

    if ($big_string != '') {
        $big_string = substr($big_string, 1);
    }

    return $big_string;
}

$now_time = time();
$arr = array('err' => 0, 'class_all_arr' => $class_all, 'uptime' => $now_time);
$jsonres = json_encode($arr);
$new_jsonres =  str_replace('\\', '\\\\', $jsonres); // 解决转json之后中文unicode无法入库
if ($rowCount == 0) $sql = "INSERT INTO `kongke` (`id`, `content`, `uptime`, `school`, `class`, `day`, `week`) VALUES (NULL ,'" . $new_jsonres . "', '" . $now_time . "', " . $school . ", " . $class_id . ", " . $day . ", " . $week . ")";
else $sql = "UPDATE `kongke` SET `content` = '" . $new_jsonres . "', `uptime` = '" . $now_time . "' WHERE `school` = " . $school . " AND `class` = " . $class_id . " AND `day` = " . $day . " AND `week` = " . $week;
$tis = $pdo->query($sql);
$tis = null;
$pdo = null;

exit($jsonres);
